/*
 * 文件名： SignRecordActivity.java
 * 描    述：积分记录
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.os.Bundle;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppContext;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.miguo.utils.SignRecorUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SignRecordActivity extends BaseActivity {

    private BaseHeadView mBaseHead;
    private ListView listView;
    private ArrayList<HashMap<String, String>> mList = null;
    private TextView mTvTotal, mTvScoreIn, mTvScoreOut;
    private String mScoreIn = "0", mScoreOut = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_record);
        findViewById();
        queryRecord();
    }

    private void findViewById() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle("积分记录");
        mTvTotal = (TextView) findViewById(R.id.record_tvTotal);
        mTvTotal.setText(AppContext.UserInfo().getScore());
        mTvScoreIn = (TextView) findViewById(R.id.record_tvScoreIn);
        mTvScoreOut = (TextView) findViewById(R.id.record_tvScoreOut);
    }

    private void initView() {
        mTvScoreIn.setText(Html.fromHtml(String.format("近一个月进账:%s",
                "<br><big><b><font color='#dd0000'>" + mScoreIn
                        + "</font></b></big>")));
        mTvScoreOut.setText(Html.fromHtml(String.format("近一个月支出:%s",
                "<br><big><b><font color='#dd0000'>" + mScoreOut
                        + "</font></b></big>")));
        listView = (ListView) findViewById(R.id.signRecord_listview);
        // 添加footview
        TextView textView = new TextView(this);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        textView.setText(R.string.setting_record_moreQurey);
        textView.setPadding(0, 20, 0, 20);
        listView.addFooterView(textView);
    }

    private void queryRecord() {

        AjaxCallBack callBack = new AjaxCallBack(mActivity) {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);
                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    mScoreIn = data.optString("income");
                    mScoreOut = String.valueOf(data.optInt("expend"));
                    JsonData score_log = data.optJson("score_log");
                    mList = SignRecorUtil.getSignRecors(score_log);
                    SimpleAdapter adapter = new SimpleAdapter(
                            SignRecordActivity.this, mList,
                            R.layout.list_sign_record, new String[]{
                            "action", "date", "score"}, new int[]{
                            R.id.listItem_signRecord_action,
                            R.id.listItem_signRecord_date,
                            R.id.listItem_signRecord_score});
                    initView();
                    listView.setAdapter(adapter);
                } else {
                    Toast.show(mContext, json.optString("info"));
                }
            }
        };
        Map<String, String> param = new HashMap<String, String>();
        param.put("token", AppContext.UserInfo().getToken());
        HttpClient.postEncryptData(Url.toRoute(Url.UserCentre.ScoreRecord, 1), param,
                callBack);
    }

}
